<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of class
 *
 * @author ������
 */
class PageUnit {

    public $data = array();
    private $template;
    private $registry;
     
    public function __construct($template) {
        $this->template = $template;
        $this->registry = Registry::getInstance();
    }
    public function __call($name, $args) {
        if (stripos($name, 'add')!==FALSE) {
            $name = strtolower(str_replace('add', '', $name));
            $this->data[$name][] = $args[0];
        }
    }

    
    private function findPageTemplate($action, $path_to_pages) {
        $controller_name = strtolower(preg_replace('/Controller/iu', '', $action->getClass()));
        $controller_method = array($action->getMethod());
        $controller_args = (array) $action->getArgs();

        $controller_method_and_args = array_merge($controller_method, $controller_args);
        $depth = 0;
        $max_depth = 0;
        $current_template = "stdpage";

        $iterator = new DirectoryIterator($path_to_pages);
        foreach ($iterator as $template) {
            if (!$template->isFile())
                continue;

            $template_name = preg_replace('/.tpl.php/iu', '', $template->getBasename());
            //echo $template_name.'<br>';


            $template_name_parts = explode('-', $template_name);

            if ($controller_name == $template_name_parts[0]) {

                array_shift($template_name_parts);

                
                if($controller_method[0] == 'index')
                {
                    array_shift($controller_method_and_args);
                }
                $depth = 0;
                foreach ($controller_method_and_args as $key => $value) {

                    if (isset($template_name_parts[$key]) && $controller_method_and_args[$key] == $template_name_parts[$key]) {
                       //echo"сравниваем ".$controller_method_and_args[$key]." c ".$template_name_parts[$key]."<br>";
                        $depth++;
                    }
                }
                if ($depth >= $max_depth && $depth==count($template_name_parts)) {
                    $current_template = $template_name;
                    $max_depth = $depth;
                }
            }
        }
        
        return $current_template;
    }
    public function setTemplate($template) {
        $this->template = $template;
    }

    public function renderPage() {
        $data = &$this->data;
        
        $action = $this->registry->router->getCurrentAction();
        $path_to_pages = DIR_TEMPLATES . $this->template . '/pages';
        $page_template = $this->findPageTemplate($action, $path_to_pages);  

        
        $data['dir_template'] = HTTP_SERVER . "templates/".$this->template."/";
        
        switch ($this->template) {
            case 'none':
                $tojson = array();
                $tojson['heap'] = @$data['heap'];
                $tojson['messages'] = @$data['messages'];
                echo json_encode($tojson);
                break;
            default :
                $data['action'] = $action;
                $this->registry->hook->invokeAll('beforepagebuild', $data);
                ob_start();
                require_once $path_to_pages . '/' . $page_template . '.tpl.php';
                $data['page'] = ob_get_contents();
                ob_end_clean();
                require_once DIR_TEMPLATES . 'standart/index.tpl.php';
                break;
        }
        exit();
    }
}

?>
